const freeMap = new Map();
freeMap.set('9.99', '9.99');
freeMap.set('19.99', '29.99');
freeMap.set('49.99', '84.98');
freeMap.set('99.99', '179.98');
freeMap.set('149.99', '284.98');
freeMap.set('199.99', '399.98');

cc.Class({
    extends: cc.Component,

    properties: {

    },

    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo

        this.rechargeWay = null;
        this.rechargeType = null;
    },

    start() {
        if (this.playerInfo.isFirstRecharge) {
            this.banButton();
            this.lobbyMain.showTips("You can participate in the First Recharge event only once per week. The event resets every Monday.")
            this.lobbyMain.hidePanel(this.node);
            return;
        }

        this.node.getChildByName("rule").active = false

        this.getFirstRechargeInfo(); //获取首充信息
    },

    onClick(event, target) {
        this.lobbyMain.playClickSound();
        if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "9.99" || target == "19.99" || target == "49.99" || target == "99.99" || target == "149.99" || target == "199.99") {
            this.goRecharge(target);
        } else if (target == "rule") {  //规则
            this.openRulePanel();
        } else if (target == "shop") {
            this.lobbyMain.openShopPanel();  //打开商城
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "close_tip") {
            this.lobbyMain.hidePanel(this.node.getChildByName("tips"));
        }
    },

    //获取首充信息返回
    onFirstRechargeInfoResult(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == 'SUCCESS') {
            let info = data.data;
            if (info.firstRechargeChannel) {
                this.rechargeWay = info.firstRechargeChannel.name;
                this.rechargeType = info.firstRechargeChannel.type;
            }

            if (info.firstRechargeOrder) {
                this.banButton();
                this.node.getChildByName("tips").active = true;

            }
        } else {
            this.lobbyMain.showTips("Failed to retrieve the first recharge channel. Please try again or contact customer service.")
            this.lobbyMain.hidePanel(this.node);
        }
    },


    //获取首充信息
    getFirstRechargeInfo() {
        let data = {
            action: "getFirstRechargeInfo",
            playerId: this.playerInfo.playerId,
            agentId: this.playerInfo.agentId
        }
        this.lobbyNet.sendMessage(data);
        this.lobbyMain.openLoading();
    },

    // 充值
    goRecharge(price) {
        let amount = price
        let giftCoin = freeMap.get(price)
        if (isNaN(amount)) {
            this.lobbyMain.showTips("Please enter a valid amount!");
            return;
        }
        if (!this.rechargeWay || !this.rechargeType) {
            this.lobbyMain.showTips("Failed to retrieve the first recharge channel. Please try again or contact customer service.");
        }
        this.lobbyMain.openLoading();
        let playerId = this.playerInfo.playerId;
        let data = {
            rechargeWay: this.rechargeWay,
            rechargeType: this.rechargeType,
            amount: amount,
            playerId: playerId,
            giftCoin: giftCoin,
            isFirst: true,
        };
        this.lobbyNet.sendPost("recharge", data, (response) => {
            if (response.code == 0) {
                this.banButton();
                this.lobbyMain.openUrl(response.data.cashierUrl);
            } else {
                this.lobbyMain.showTips("Failed to initiate payment.");
            }
        });
    },

    // 打开规则面板
    openRulePanel() {
        let rulePanel = this.node.getChildByName("rule");
        if (rulePanel.active) {
            this.lobbyMain.hidePanel(rulePanel);
        } else {
            this.lobbyMain.showPanel(rulePanel);
        }
    },


    //禁用按钮
    banButton() {
        this.node.getChildByName("guang1").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("guang2").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("guang3").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("guang4").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("guang5").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("guang6").getChildByName("but_mai").getComponent(cc.Button).interactable = false;
    }

    // update (dt) {},
});
